package greetings

import (
	"fmt"
	"sync"
)

var i = 0
var wg sync.WaitGroup

// MultipleThreadDemo 多线程 同步执行代码
func MultipleThreadDemo() {

	for i := 0; i < 100; i++ {
		wg.Add(1) // 启动一个goroutine就登记+1
		go helloMultipleThread()
	}
	wg.Wait() // 等待所有登记的goroutine都结束
	fmt.Println("this is a test thread end finally i: ", i)
}

func helloMultipleThread() {
	i++
	fmt.Println("this is a test thread : ", i)
	defer wg.Done()
}
